package io.jpress.core.flowable;

import io.jboot.utils.StrUtil;
import io.jpress.commons.flowable.FlowConsts;
import org.flowable.bpmn.model.ExtensionAttribute;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.engine.RuntimeService;
import org.flowable.task.api.Task;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 待办事项
 */
public class FlowTodoItemVO implements Serializable {

    private String taskId;
    private String name;
    private String contentType;
    private Object contentId;
    private String contentTitle;
    private String comment;
    private Date created;

    private List<FlowTodoItemAction> actions = new ArrayList();

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public Object getContentId() {
        return contentId;
    }

    public void setContentId(Object contentId) {
        this.contentId = contentId;
    }

    public String getContentTitle() {
        return contentTitle;
    }

    public void setContentTitle(String contentTitle) {
        this.contentTitle = contentTitle;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public List<FlowTodoItemAction> getActions() {
        return actions;
    }

    public void setActions(List<FlowTodoItemAction> actions) {
        this.actions = actions;
    }

    public static FlowTodoItemVO fromTask(Task task) {
        RuntimeService runtimeService = FlowableUtil.getRuntimeService();
        FlowTodoItemVO item = new FlowTodoItemVO();
        item.setTaskId(task.getId());
        item.setName(task.getName());
        item.setComment((String) runtimeService.getVariable(task.getProcessInstanceId(), FlowConsts.VAR_COMMENT));
        item.setContentId(runtimeService.getVariable(task.getProcessInstanceId(), FlowConsts.VAR_CONTENT_ID));
        item.setContentType((String) runtimeService.getVariable(task.getProcessInstanceId(), FlowConsts.VAR_CONTENT_TYPE));
        item.setContentTitle((String) runtimeService.getVariable(task.getProcessInstanceId(), FlowConsts.VAR_CONTENT_TITLE));
        item.setCreated(task.getCreateTime());


        FlowElement element = FlowableUtil.getFlowElementByDefKey(task.getProcessDefinitionId(), task.getTaskDefinitionKey());

        Map<String, List<ExtensionAttribute>> attributes = element.getAttributes();
        attributes.forEach((key, extensionAttributes) -> {

            //操作按钮的配置
            if (key != null && key.endsWith("Action")) {
                String action = key.substring(0, key.length() - 6);

                if (extensionAttributes != null && extensionAttributes.size() > 0) {
                    ExtensionAttribute extensionAttribute = extensionAttributes.get(0);
                    String attrValue = extensionAttribute.getValue(); // value == "true"

                    //操作按钮有配置，说明就显示，若流程图里没勾选这个操作按钮，则没有任何值
                    if (StrUtil.isNotBlank(attrValue)) {

                        String text = FlowConsts.getStatusString(action);
                        String value = action;

                        FlowTodoItemAction flowTodoItemAction = new FlowTodoItemAction(text, value);
                        item.actions.add(flowTodoItemAction);
                    }
                }
            }
        });


        return item;
    }


    public static List<FlowTodoItemVO> formTaskList(List<Task> tasks) {
        List<FlowTodoItemVO> flowTodoItems = new ArrayList<>(tasks.size());
        for (Task task : tasks) {
            flowTodoItems.add(fromTask(task));
        }
        return flowTodoItems;
    }
}
